home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.03 Mar 93 / SoundINIT / SoundINIT.c next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  3.4 KB  |  103 lines  |  [TEXT/KAHL]

  1. /* Both THINK-C 5.0 and MPW 3.2 includes: */
  2. #include <Types.h>
  3. #include <Resources.h>
  4. #include <Events.h>
  5. #include <Sound.h>
  6.  
  7. /*  The next couple lines are proto types.  */
  8. /*  KeyPressed returns true if a key is pressed */
  9. Boolean    KeyPressed( 
  10.     short keyNum, unsigned char *keyMapPtr);
  11. /*  main plays sound ID # 129 until its done or a key is pressed */
  12. void        main( void);
  13.  
  14. /*    Every program needs a bug, this program currently has none, as you can clearly see because DEBUG is false.  I encourage you to add to this code until it has atleast three good bugs.  */
  15. #define    DEBUG    false
  16.  
  17. /*  The KillKey is currently set to the space bar
  18.     (0x31).  The list of keys can be found in InsideMac’s Vol. I pg. 251; Vol. IV pg. 250, and
  19. Vol. VI pg. 191-2 */
  20. #define    KILLKEY    0x31
  21.  
  22. /*  kSndRsrc holds the resource #, 129 is compatible with cdev’s, see Dave Mark’s “Macintosh C Programming Primer Vol. II.”  See chapter 3. */
  23. #define    kSndRsrc        129
  24.  
  25. /********************* main *********************/
  26. void main()
  27. {
  28.     SCStatus    sndStatus;         // Used while snd plays
  29.     Handle        theSnd;                 // the snd resource
  30.     OSErr        playStatus;     // standard error var
  31.     KeyMap        theKeys;                // bit map of key presses
  32.     // the channel through which sound will come
  33.     SndChannelPtr theChannel;
  34.     
  35. /*    If there are any bugs, then go to the debugger first. */
  36. #if DEBUG
  37.     Debugger();
  38. #endif
  39.  
  40. /*    Check to see if the KillKey is pressed */
  41.     GetKeys( &theKeys); // Read the keyboard
  42.     if(KeyPressed(KILLKEY,
  43.                         (unsigned char*)theKeys) == false)
  44.     {    // if the space bar is not pressed, continue
  45.     /* Set the channel ptr to NIL */
  46.         theChannel = (SndChannelPtr) 0l;
  47.     /* Allocate a new channel */
  48.         playStatus = SndNewChannel( &theChannel, 0,
  49.                         (long int)0, (SndCallBackProcPtr) 0);
  50.     /* As long as that worked fine... */
  51.         if( playStatus == noErr)
  52.         {
  53.         /* Read the sound resource into memory */
  54.             theSnd = GetResource('snd ', kSndRsrc);
  55.         /* 0 is returned if there was a res error */
  56.             if( theSnd)
  57.             {
  58.             /* SoundPlay needs the channel, the sound, */
  59.                 playStatus = SndPlay( theChannel, theSnd,                                 true);  // and an ASYNC flag
  60.             /* Sound Channel Status will return, among
  61.              other things, the status of the sound */
  62.                 playStatus = SndChannelStatus( theChannel,
  63.                                 sizeof( sndStatus), &sndStatus);
  64.             /* While the KillKey isn’t pressed, &
  65.                 the sound is playing (i.e., busy) */
  66.                 while(KeyPressed(KILLKEY,
  67.                                 (unsigned char*)theKeys) == 0 &&
  68.                                 sndStatus.scChannelBusy == true)
  69.                 {
  70.                 /* Read the keyboard */
  71.                     GetKeys( &theKeys);
  72.                 /* Get the sound channel status */
  73.                     playStatus = SndChannelStatus(
  74.                                     theChannel, sizeof( sndStatus),
  75.                                     &sndStatus);
  76.                 }    // END while
  77.             }    // END if( theSnd)
  78.         /* Kill the sound (true == dispose now) */
  79.             playStatus = SndDisposeChannel( theChannel, true);
  80.         }  // END if( playStatus == true)
  81.     }    // END if( KeyPressed( ...) == false
  82. }    // END main() {...}
  83.  
  84.  
  85. /***************** KeyPressed *****************/
  86. /*  The following algorithm I typed directly
  87.     from Semantic’s THINK Reference.  An excellent
  88.     tool that I highly recommend! Only $69 */
  89. //  returns
  90. //    true == the key is pressed
  91. //    false == the key is not pressed
  92. Boolean    KeyPressed( short keyNum, 
  93.                         unsigned char *keyMapPtr)
  94. {
  95.     /* This convoluted but functional and tight
  96.         algorithm determines if the key keyNum
  97.         is pressed... Read InsideMac’s Vol. I pg. 251;
  98.         Vol. IV pg. 250, and Vol. VI pg. 191-2 */
  99.     return( (keyMapPtr[keyNum >> 3] 
  100.                         >> (keyNum & 7) ) & 1);
  101. }
  102.  
  103.